<?php 

//$pag	= new PaginationHelper($this->data['list_total'], $this->data['pagination']['limit'], $this->data['pagination']['page_show'], $this->data['pagination']['page'], 'amounts_list');
/*
 * $this->data['pagination']	= array(
				'page'		=> $request->getParameter('page', 1),
				'limit'		=> $request->getParameter('limit', 10),
				'page_show'	=> 5,
				'total'		=> 0,
				'router'	=> '',
				'params'	=> array()
		);
 */
		
class PaginationHelper {
	private $item_total;
	private $item_per_page;
	private $page_current;
	private $page_total;
	private $page_router;
	private $page_show_limit;
	private $params = array();
	public function __construct($item_total, $item_per_page, $page_show_limit, $page_current, $page_router, $params = array()){
		$this->item_per_page = intval($item_per_page);
		$this->item_total = intval($item_total);
		$this->page_current = intval($page_current)>0?intval($page_current):1;
		$this->page_router = $page_router;
		$this->page_show_limit = $page_show_limit;
		$this->params = $params;
		$this->page_total = ceil($this->item_total / $this->item_per_page);
	}
	/*public function __construct($data = array()){
		$this->item_per_page = intval($data['limit']);
		$this->item_total = intval($data['total']);
		$this->page_current = intval($data['page'])>0?intval($data['page']):1;
		$this->page_router = $data['router'];
		$this->page_show_limit = intval($data['page_show']);
		$this->params = $data['params'];
		$this->page_total = ceil($this->item_total / $this->item_per_page);
	}*/
	public function getPageTotal(){
		return $this->page_total;
	}
	public function isNextPage(){
		return $this->page_current < $this->page_total;
	}
	public function getNextPage(){
		if(self::isNextPage()){
			return $this->page_current+1;
		}
		return $this->page_current;
	}
	public function getPaginations(){
		if($this->page_current > $this->page_total) return array();
		sfContext::getInstance()->getConfiguration()->loadHelpers('Url');
		$pag = array();
		$page_div = floor($this->page_show_limit / 2);
		$params = $this->params;
		if($this->page_total > $this->page_show_limit+2){
			if($this->page_current-$page_div>1){
				$params['page'] = 1;
				$pag[] = array(
						'page_num' 	 => 1,
						'page_class' => 'page-normal',
						'url'		 => url_for2($this->page_router,$params)
				);
			}
			if($this->page_current-$page_div>2){
				$pag[] = array(
						'page_num' 	 => '.',
						'page_class' => 'page-dot',
						'url'		 => ''
				);
				$pag[] = array(
						'page_num' 	 => '.',
						'page_class' => 'page-dot',
						'url'		 => ''
				);
			}
			
			for($i=($this->page_current-$page_div>=1?$this->page_current-$page_div:1); $i<$this->page_current;$i++){
				$params['page'] = $i;
				$pag[] = array(
						'page_num' 	 => $i,
						'page_class' => 'page-normal',
						'url'		 => url_for2($this->page_router,$params)
				);
			}
			$params['page'] = $this->page_current;
			$pag[] = array(
					'page_num' 	 => $this->page_current,
					'page_class' => 'page-current',
					'url'		 => url_for2($this->page_router,$params)
			);
			for($i=$this->page_current+1; ($i<=$this->page_current+$page_div) && $i<=$this->page_total;$i++){
				$params['page'] = $i;
				$pag[] = array(
						'page_num' 	 => $i,
						'page_class' => 'page-normal',
						'url'		 => url_for2($this->page_router,$params)
				);
			}
			if($this->page_current+$page_div<$this->page_total-1){
				$pag[] = array(
						'page_num' 	 => '.',
						'page_class' => 'page-dot',
						'url'		 => ''
				);
				$pag[] = array(
						'page_num' 	 => '.',
						'page_class' => 'page-dot',
						'url'		 => ''
				);
			}
			if($this->page_current+$page_div<$this->page_total){
				$params['page'] = $this->page_total;
				$pag[] = array(
						'page_num' 	 => $this->page_total,
						'page_class' => 'page-normal',
						'url'		 => url_for2($this->page_router,$params)
				);
			}
		}else{
			for($i=0; $i<$this->page_total;$i++){
				$params['page'] = $i+1;
				$pag[] = array(
						'page_num' 	 => $i+1,
						'page_class' => ($this->page_current==($i+1)?'page-current':'page-normal'),
						'url'		 => url_for2($this->page_router,$params)
				);
			}
		}
		return $pag;
	}
	
}
class PaginationHelper2 {
	private $item_total;
	private $item_per_page;
	private $page_current;
	private $page_total;
	private $page_router;
	private $page_show_limit;
	private $params = array();
	public function __construct($data = array()){
	$this->item_per_page = intval($data['limit']);
	$this->item_total = intval($data['total']);
	$this->page_current = intval($data['page'])>0?intval($data['page']):1;
	$this->page_router = $data['router'];
	$this->page_show_limit = intval($data['page_show']);
	$this->params = $data['params'];
	$this->page_total = ceil($this->item_total / $this->item_per_page);
	}
	public function getPageTotal(){
		return $this->page_total;
	}
	public function isNextPage(){
		return $this->page_current < $this->page_total;
	}
	public function getNextPage(){
		if(self::isNextPage()){
			return $this->page_current+1;
		}
		return $this->page_current;
	}
	public function getPaginations(){
		if($this->page_current > $this->page_total) return array();
		sfContext::getInstance()->getConfiguration()->loadHelpers('Url');
		$pag = array();
		$page_div = floor($this->page_show_limit / 2);
		$params = $this->params;
		if($this->page_total > $this->page_show_limit+2){
			if($this->page_current-$page_div>1){
				$params['page'] = 1;
				$pag[] = array(
						'page_num' 	 => 1,
						'page_class' => 'page-normal',
						'url'		 => url_for2($this->page_router,$params)
				);
			}
			if($this->page_current-$page_div>2){
				$pag[] = array(
						'page_num' 	 => '.',
						'page_class' => 'page-dot',
						'url'		 => ''
				);
				$pag[] = array(
						'page_num' 	 => '.',
						'page_class' => 'page-dot',
						'url'		 => ''
				);
			}
				
			for($i=($this->page_current-$page_div>=1?$this->page_current-$page_div:1); $i<$this->page_current;$i++){
				$params['page'] = $i;
				$pag[] = array(
						'page_num' 	 => $i,
						'page_class' => 'page-normal',
						'url'		 => url_for2($this->page_router,$params)
				);
			}
			$params['page'] = $this->page_current;
			$pag[] = array(
					'page_num' 	 => $this->page_current,
					'page_class' => 'page-current',
					'url'		 => url_for2($this->page_router,$params)
			);
			for($i=$this->page_current+1; ($i<=$this->page_current+$page_div) && $i<=$this->page_total;$i++){
				$params['page'] = $i;
				$pag[] = array(
						'page_num' 	 => $i,
						'page_class' => 'page-normal',
						'url'		 => url_for2($this->page_router,$params)
				);
			}
			if($this->page_current+$page_div<$this->page_total-1){
				$pag[] = array(
						'page_num' 	 => '.',
						'page_class' => 'page-dot',
						'url'		 => ''
				);
				$pag[] = array(
						'page_num' 	 => '.',
						'page_class' => 'page-dot',
						'url'		 => ''
				);
			}
			if($this->page_current+$page_div<$this->page_total){
				$params['page'] = $this->page_total;
				$pag[] = array(
						'page_num' 	 => $this->page_total,
						'page_class' => 'page-normal',
						'url'		 => url_for2($this->page_router,$params)
				);
			}
		}else{
			for($i=0; $i<$this->page_total;$i++){
				$params['page'] = $i+1;
				$pag[] = array(
						'page_num' 	 => $i+1,
						'page_class' => ($this->page_current==($i+1)?'page-current':'page-normal'),
						'url'		 => url_for2($this->page_router,$params)
				);
			}
		}
		return $pag;
	}

}